//
//  ViewController.m
//  ios_charts
//
//  Created by 刘三丰 on 2018/5/8.
//  Copyright © 2018年 刘三丰. All rights reserved.
//

#import "ViewController.h"

#import "iOS-Echarts.h"

#import "RMMapper.h"
@interface ViewController ()

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong)  PYZoomEchartsView *kEchartView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.kEchartView = [[PYZoomEchartsView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width , 500)];
    
    self.kEchartView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.kEchartView];
    
}


#pragma mark -----  标准折线状图

- (IBAction)showLineDemo:(id)sender {
    /** 图表选项 */
    PYOption *option = [[PYOption alloc] init];
    //是否启用拖拽重计算特性，默认关闭
    option.calculable = NO;
    //数值系列的颜色列表(折线颜色)
    option.color = @[@"#20BCFC", @"#ff6347"];
    // 图标背景色
    // option.backgroundColor = [[PYColor alloc] initWithColor:[UIColor orangeColor]];
    
    
    /** 提示框 */
    PYTooltip *tooltip = [[PYTooltip alloc] init];
    // 触发类型 默认数据触发
    tooltip.trigger = @"axis";
    // 竖线宽度
    tooltip.axisPointer.lineStyle.width = @1;
    // 提示框 文字样式设置
    tooltip.textStyle = [[PYTextStyle alloc] init];
    tooltip.textStyle.fontSize = @12;
    
    // 添加到图标选择中
    option.tooltip = tooltip;
    
    
    /** 图例 */
    PYLegend *legend = [[PYLegend alloc] init];
    // 设置数据
    legend.data = @[@"挂牌价",@"成交价"];
    
    // 添加到图标选择中
    option.legend = legend;
    
    
    /** 直角坐标系内绘图网格, 说明见下图 */
    PYGrid *grid = [[PYGrid alloc] init];
    // 左上角位置
    grid.x = @(50);
    grid.y = @(60);
    // 右下角位置
    grid.x2 = @(30);
    grid.y2 = @(50);
    grid.borderWidth = @(0);
    
    // 添加到图标选择中
    option.grid = grid;
    
    /** X轴设置 */
    PYAxis *xAxis = [[PYAxis  alloc] init];
    //横轴默认为类目型(就是坐标自己设置)
    xAxis.type = @"category";
    // 起始和结束两端空白
    xAxis.boundaryGap = @(YES);
    // 分隔线
    xAxis.splitLine.show = NO;
    xAxis.splitNumber = @4;
    // 坐标轴线
    xAxis.axisLine.show = NO;
    // X轴坐标数据
    xAxis.data = @[@"1月", @"2月", @"3月", @"4月", @"5月", @"6月", @"7月", @"8月", @"9月", @"10月", @"11月", @"12月" ].mutableCopy;
    // 坐标轴小标记
    xAxis.axisTick = [[PYAxisTick alloc] init];
    xAxis.axisTick.show = YES;
    
    // 添加到图标选择中
    option.xAxis = [[NSMutableArray alloc] initWithObjects:xAxis, nil];
    
    
    /** Y轴设置 */
    PYAxis *yAxis = [[PYAxis alloc] init];
    yAxis.axisLine.show = NO;
    // 纵轴默认为数值型(就是坐标系统生成), 改为 @"category" 会有问题, 读者可以自行尝试
    yAxis.type = @"value";
    // 分割段数，默认为5
    yAxis.splitNumber = @4;
    // 分割线类型
    // yAxis.splitLine.lineStyle.type = @"dashed";   //'solid' | 'dotted' | 'dashed' 虚线类型
    
    //单位设置,  设置最大值, 最小值
    // yAxis.axisLabel.formatter = @"{value} k";
    // yAxis.max = @9000;
    // yAxis.min = @5000;
    
    
    // 添加到图标选择中  ( y轴更多设置, 自行查看官方文档)
    option.yAxis = [[NSMutableArray alloc] initWithObjects:yAxis, nil];
    
    
    /** 定义坐标点数组 */
    NSMutableArray *seriesArr = [NSMutableArray array];
    
    /** 第一条折线设置 */
    PYCartesianSeries *series1 = [[PYCartesianSeries alloc] init];
    series1.name = @"挂牌价";
    // 类型为折线
    series1.type = @"line";
    // 坐标点大小
    series1.symbolSize = @(1.5);
    // 坐标点样式, 设置连线的宽度
    series1.itemStyle = [[PYItemStyle alloc] init];
    series1.itemStyle.normal = [[PYItemStyleProp alloc] init];
    series1.itemStyle.normal.lineStyle = [[PYLineStyle alloc] init];
    series1.itemStyle.normal.lineStyle.width = @(1.5);
    // 添加坐标点 y 轴数据 ( 如果某一点 无数据, 可以传 @"-" 断开连线 如 : @[@"7566", @"-", @"7571"]  )
    series1.data = @[@"7566", @"7619", @"7571", @"7670", @"7528", @"7640", @"7472", @"7800", @"8058", @"7972", @"7606", @"7710"];
    
    [seriesArr addObject:series1];
    
    /** 第二条折线设置 */
    PYCartesianSeries *series2 = [[PYCartesianSeries alloc] init];
    series2.name = @"成交价";
    series2.type = @"line";
    series2.symbolSize = @(1.5);
    series2.itemStyle = [[PYItemStyle alloc] init];
    series2.itemStyle.normal = [[PYItemStyleProp alloc] init];
    series2.itemStyle.normal.lineStyle = [[PYLineStyle alloc] init];
    series2.itemStyle.normal.lineStyle.width = @(1.5);
    series2.data = @[@"7066", @"7119", @"7471", @"7470", @"7228", @"7340", @"7402", @"7600", @"7858", @"7772", @"7506", @"7310"];
    [seriesArr addObject:series2];
    
    [option setSeries:seriesArr];
    
    
    // 图表选项添加到图表上
    [self.kEchartView setOption:option];
    [_kEchartView loadEcharts];


    
}

#pragma mark -----  标准柱状图

- (IBAction)showBasicColumnDemo:(id)sender {
    
    NSString *basicColumnJson = [NSString stringWithFormat:@"{\"grid\":{\"x\":30,\"x2\":45},\"title\":{\"text\":\"%@\"},\"tooltip\":{\"trigger\":\"axis\"},\"legend\":{\"data\":[\"%@\"],\"itemWidth\":\"0\"},\"toolbox\":{\"show\":false,\"feature\":{\"mark\":{\"show\":false},\"dataView\":{\"show\":false,\"readOnly\":true},\"magicType\":{\"show\":false,\"type\":[\"line\",\"bar\"]},\"restore\":{\"show\":false},\"saveAsImage\":{\"show\":false}}},\"calculable\":false,\"xAxis\":[{\"type\":\"category\",\"data\":[\"2013-10-11\",\"2013-12-11\",\"2014-1-11\"]}],\"yAxis\":[{\"type\":\"value\"}],\"series\":[{\"name\":\"%@\",\"type\":\"bar\",\"data\":[135.6,462.2,32.6],\"markPoint\":{\"data\":[{\"type\":\"max\",\"name\":\"最大值\"},{\"type\":\"min\",\"name\":\"最小值\"}]},\"markLine\":{\"data\":[{\"type\":\"average\",\"name\":\"平均值\"}]}}]}",@"门诊",@"门诊",@"门诊"];
    NSData *jsonData = [basicColumnJson dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
    PYOption *option = [RMMapper objectWithClass:[PYOption class] fromDictionary:jsonDic];
    // 图表选项添加到图表上
    [self.kEchartView setOption:option];
    [_kEchartView loadEcharts];


}
#pragma mark -----  标准饼状图

- (IBAction)showBasicPieDemo:(id)sender {
    
    NSString *basicPieJson = @"{\"title\":{\"text\":\"某站点用户访问来源\",\"subtext\":\"纯属虚构\",\"x\":\"center\"},\"tooltip\":{\"trigger\":\"item\",\"formatter\":\"{a} <br/>{b} : {c} ({d}%)\"},\"legend\":{\"orient\":\"vertical\",\"x\":\"left\",\"data\":[\"直接访问\",\"邮件营销\",\"联盟广告\",\"视频广告\",\"搜索引擎\"]},\"toolbox\":{\"show\":false,\"feature\":{\"mark\":{\"show\":false},\"dataView\":{\"show\":false,\"readOnly\":false},\"magicType\":{\"show\":false,\"type\":[\"pie\",\"funnel\"],\"option\":{\"funnel\":{\"x\":\"25%\",\"width\":\"50%\",\"funnelAlign\":\"left\",\"max\":1548}}},\"restore\":{\"show\":true},\"saveAsImage\":{\"show\":false}}},\"calculable\":false,\"series\":[{\"name\":\"访问来源\",\"type\":\"pie\",\"radius\":\"55%\",\"center\":[\"50%\",\"60%\"],\"data\":[{\"value\":335,\"name\":\"直接访问\"},{\"value\":310,\"name\":\"邮件营销\"},{\"value\":234,\"name\":\"联盟广告\"},{\"value\":135,\"name\":\"视频广告\"},{\"value\":1548,\"name\":\"搜索引擎\"}]}]}";
    NSData *jsonData = [basicPieJson dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
    PYOption *option = [RMMapper objectWithClass:[PYOption class] fromDictionary:jsonDic];
    
    [_kEchartView setOption:option];
    [_kEchartView loadEcharts];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
